﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrmDocman.DataCore;

/// <summary>
/// Summary description for BannerQuangCaoHelper
/// </summary>
public class BannerQuangCaoHelper : TWSData
{
    public BannerQuangCaoHelper()
	{
        m_dao = DataBridge.GetInstance();
	}

    public BannerQuangCaoDataSet LoadBannerQuangCao()
    {

        BannerQuangCaoDataSet lspDs = new BannerQuangCaoDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.BannerQuangCao);
        sql += " order by ThuTu ";

        m_dao.FillDataTable(lspDs.BannerQuangCao, sql);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public BannerQuangCaoDataSet LoadBannerQuangCaoTheoLoaiSanPham(int loaisanpham)
    {

        BannerQuangCaoDataSet lspDs = new BannerQuangCaoDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.BannerQuangCao);
        sql += " where LoaiSanPham = " + loaisanpham + " order by ThuTu";

        m_dao.FillDataTable(lspDs.BannerQuangCao, sql);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public BannerQuangCaoDataSet LoadBannerQuangCaoTheoLoaiSanPhamCha(int loaisanpham)
    {

        BannerQuangCaoDataSet lspDs = new BannerQuangCaoDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.BannerQuangCao);
        sql += " where LoaiSanPham in ( select LoaiSanPhamChaID from dbo.LoaiSanPham where LoaiSanPhamID = " + loaisanpham + " ) order by ThuTu";

        m_dao.FillDataTable(lspDs.BannerQuangCao, sql);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public BannerQuangCaoDataSet LoadBannerQuangCao(int BannerQuangCaoID)
    {

        BannerQuangCaoDataSet lspDs = new BannerQuangCaoDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.BannerQuangCao);
        sql += " where ID = " + BannerQuangCaoID;

        m_dao.FillDataTable(lspDs.BannerQuangCao, sql);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public bool SaveBannerQuangCao(BannerQuangCaoDataSet lspDs)
    {
        m_dao.UpdateDataTable(lspDs.BannerQuangCao);
        m_dao.Connection.CloseConnection();
        return true;

    }

    public BannerQuangCaoDataSet LoadBannerQuangCao(int page, int pageSize, string sortColumn, string sortOrder,int loaisanpham)
    {

        BannerQuangCaoDataSet spDs = new BannerQuangCaoDataSet();
        string sql = "DECLARE @PageNum AS INT; ";
        sql += "DECLARE @PageSize AS INT; ";
        sql += "SET @PageNum = " + page + "; ";
        sql += "SET @PageSize = " + pageSize + "; ";
        sql += "WITH OrdersRN AS ( ";
        sql += "SELECT ROW_NUMBER() OVER(ORDER BY " + sortColumn + " " + sortOrder + ") AS RowNum, * ";
        sql += "FROM dbo.BannerQuangCao where LoaiSanPham=" + loaisanpham + " ) ";
        sql += "select * from OrdersRN ";
        sql += " WHERE RowNum BETWEEN (@PageNum - 1) * @PageSize + 1 AND @PageNum * @PageSize ";
        sql += " order by " + sortColumn + " " + sortOrder;

        m_dao.FillDataTable(spDs.BannerQuangCao, sql);
        m_dao.Connection.CloseConnection();
        return spDs;
    }

    public BannerQuangCaoDataSet LoadBannerQuangCao(int page, int pageSize, string sortColumn, string sortOrder)
    {

        BannerQuangCaoDataSet spDs = new BannerQuangCaoDataSet();
        string sql = "DECLARE @PageNum AS INT; ";
        sql += "DECLARE @PageSize AS INT; ";
        sql += "SET @PageNum = " + page + "; ";
        sql += "SET @PageSize = " + pageSize + "; ";
        sql += "WITH OrdersRN AS ( ";
        sql += "SELECT ROW_NUMBER() OVER(ORDER BY " + sortColumn + " " + sortOrder + ") AS RowNum, * ";
        sql += "FROM dbo.BannerQuangCao ) ";
        sql += "select * from OrdersRN ";
        sql += " WHERE RowNum BETWEEN (@PageNum - 1) * @PageSize + 1 AND @PageNum * @PageSize ";
        sql += " order by " + sortColumn + " " + sortOrder;

        m_dao.FillDataTable(spDs.BannerQuangCao, sql);
        m_dao.Connection.CloseConnection();
        return spDs;
    }

    public BannerQuangCaoDataSet LoadBannerTheoLoai(int type)
    {
        LoaiSanPhamHelper lspHP = new LoaiSanPhamHelper();
        LoaiSanPhamDataSet lspDs = lspHP.GetLoaiSanPhamCha(type);

        if (lspDs.LoaiSanPham.Rows.Count > 0)
        {
            if (lspDs.LoaiSanPham[0].IsLoaiSanPhamChaIDNull())
            {
                return LoadBannerQuangCaoTheoLoaiSanPham(lspDs.LoaiSanPham[0].LoaiSanPhamID);
            }
            else
            {
                return LoadBannerQuangCaoTheoLoaiSanPham(lspDs.LoaiSanPham[0].LoaiSanPhamChaID);
            }
        }
        return LoadBannerQuangCaoTheoLoaiSanPham(type);
        //if (type == 0)
        //{
        //    return LoadBannerQuangCao();
        //}
        //if (!new LoaiSanPhamHelper().IsLoaiSanPhamCha(type))
        //{
        //    return LoadBannerQuangCaoTheoLoaiSanPhamCha(type);
        //}
        //return LoadBannerQuangCaoTheoLoaiSanPham(type);
    }
}